package com.jack.hhitseat.task;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.jack.hhitseat.bean.AppBean;
import com.jack.hhitseat.bean.User;
import com.jack.hhitseat.service.HttpClient;
import com.jack.hhitseat.service.impl.LogServiceImpl;
import com.jack.hhitseat.utils.MyUtils;

public class YzmRunnable extends Thread{
//	private final Logger logger = LoggerFactory.getLogger(MyRunnable.class);
	private static LogServiceImpl logServiceImpl = (LogServiceImpl) AppBean.getBean("logServiceImpl");
	private static HttpClient httpClient = new HttpClient();
	private User user;
	private ConcurrentMap<String, String> sessionMap = new ConcurrentHashMap<>();
	private final static String KEY = "0";
	private final static  String BOOK_URL = "http://seat.hhit.edu.cn/ClientWeb/pro/ajax/reserve.aspx?" +
			"&dev_id={dev_id}" + "&type={type}" + "&start={start}" + "&end={end}" +
			"&start_time={start_time}" + "&end_time={end_time}"+"&number={number}" + "&act={act}" +
			"&_={_}"; 
	
	public YzmRunnable() {
	}
	public YzmRunnable(User user
			, ConcurrentMap<String, String> sessionMap) {
		this.user = user;
		this.sessionMap = sessionMap;
	}
	
	@Override
	public void run() {
		final int COUNT = 300;
		String stuNum = user.getStuNum();
		String result = "";
		String session = sessionMap.get(stuNum);
		int i = 0;
		String seatNumb = user.getSeat().split(",")[0].split("=")[0];
		String yyrq = MyUtils.getNextDate(); 
		
	    Map<String, Object> qzParparamMap = new HashMap<String, Object>();
		qzParparamMap.put("type","dev");
		qzParparamMap.put("start",yyrq+"+08:00");
		//qzParparamMap.put("end",yyrq+"+22:00");
		qzParparamMap.put("end",yyrq+"+22:00");		//暑假时间
		qzParparamMap.put("start_time","800"); 
		//qzParparamMap.put("end_time","2200");
		qzParparamMap.put("end_time","2200");		//暑假时间
		qzParparamMap.put("number", KEY);			//万能钥匙Key
		qzParparamMap.put("act","set_resv");
		qzParparamMap.put("dev_id", seatNumb); 
		
		String sjc = (System.currentTimeMillis()+120000)+"";
		qzParparamMap.put("_",sjc);
		
		for (i = 1; i <= COUNT; i++) {
			result = httpClient.qz2(BOOK_URL, session, qzParparamMap);
			if(!result.contains("验证码")) {
				if(result.contains("成功")) {
					logServiceImpl.addLog(stuNum, seatNumb);
					break;
				}
				//logger.warn("【验证码】==={}==={}", i, user.getUserName());
				break;
			}else {
				httpClient.yzm(session);
			}
		}
	}
}
